package com.midian.service.user.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.common.system.SysStatic;
import com.midian.model.user.User;
import com.midian.service.common.BaseService;
import com.midian.service.user.IUserService;

@Service("userService")
public class UserServiceImpl extends BaseService implements IUserService {
	/**用户DAO*/
	private BaseDaoTemplet<User> userDAO;
	
	@Resource(name = "userDAO")
	public void setUserDAO(BaseDaoTemplet<User> userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public User add(User target) throws BizException {
		this.userDAO.add(target);
		return target;
	}

	@Override
	public void update(User target) throws BizException {
		this.userDAO.update(target);
	}
	
	/**
	 * 更新用户信息
	 * @param user
	 * @throws Exception
	 */
	@Override
	public void updateUser(User user) throws Exception {
		this.userDAO.updateByConvertHql(user);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.userDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.userDAO.delete(ids);
	}

	@Override
	public User get(Serializable id) throws BizException {
		String hql = "from User o where o.id = ? and o.delFlag = ?";
		return (User) this.userDAO.findObject(hql, new Object[] {id,SysStatic.NORMALDELFLAG});
	}

	@Override
	public List<User> showList(User target, Pager pager,
			List<OrderHelper> helper) throws BizException {
		return this.userDAO.find(target, getSearchHelper(), helper, pager);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.userDAO.opDelFlag(ids, status);
	}
	
	/**
	 * 获取会员信息通过token
	 * @param token token
	 * @param userId 会员ID
	 * @return
	 * @throws BizException
	 */
	@Override
	public User getUserByToken(String token, String userId) {
		String hql = "from User o where o.token=? and o.id = ? and o.delFlag = ?";
		return (User) this.userDAO.findObject(hql, new Object[] {token, userId,SysStatic.NORMALDELFLAG});
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		return null;
	}

	/**
	 * 根据帐号，检测是否帐号已经存在
	 * @param account 帐号
	 * @return
	 * @throws BizException
	 */
	@Override
	public boolean isExistUserByAccount(String account) {
		String hql = "select count(o) from User o where o.account = ? and o.delFlag = ?";
		return this.userDAO.getCount(hql, new Object[]{account,SysStatic.NORMALDELFLAG}) > 0;
	}
	/**
	 * 登录
	 * @param account 帐号
	 * @param password 密码
	 * @return
	 * @throws BizException
	 */
	@Override
	public User login(String account, String password) {
		String hql = "from User o where o.account = ? and o.password = ? and o.delFlag = ?";
		return (User) this.userDAO.findObject(hql, new Object[]{account, password,SysStatic.NORMALDELFLAG});
	}
	/**
	 * 根据第三方登录id和来源查找用户
	 * @param thirdId 第三方登录id
	 * @param userfrom 来源
	 * @return
	 * @throws BizException
	 */
	@Override
	public User getUserByThirdIdAndFrom(String thirdId, Integer userfrom) {
		String hql = "from User o where o.thirdId = ? and o.userfrom = ? and o.delFlag = ?";
		return (User) this.userDAO.findObject(hql, new Object[]{thirdId, userfrom,SysStatic.NORMALDELFLAG});
	}
	/**
	 * 更新用户的Token信息
	 * @param token token
	 * @param userId 用户id
	 * @return
	 * @throws BizException
	 */
	@Override
	public void updateUserTokenById(String token, String userId) throws BizException {
		if(StringUtils.isNotBlank(token) && userId != null) {
			String hql = "update User o set o.token = ?,o.tokenTime = ? where o.id = ? and o.delFlag = ?";
			this.userDAO.update(hql, new Object[]{token, new Date(), userId,SysStatic.NORMALDELFLAG});
		}
	}
	/**
	 * 更新用户密码
	 * @param account 帐号
	 * @param pwd 密码
	 * @param code 验证码
	 * @return
	 * @throws BizException
	 */
	@Override
	public int updatePwdByUserId(String userId, String pwd) throws BizException {
		String hql = "update User o set o.password = ? where o.id = ? and o.delFlag = ?";
		return this.userDAO.update(hql, new Object[]{pwd, userId,SysStatic.NORMALDELFLAG});
	}
	
	/**
	 * 更新用户密码（找回密码）
	 * @param phone 电话号码（帐号）
	 * @param pwd 密码
	 * @return
	 * @throws BizException
	 */
	@Override
	public int updatePwdByPhone(String phone, String pwd) throws BizException {
		String hql = "update User o set o.password = ? where o.account = ? and o.delFlag = ?";
		return this.userDAO.update(hql, new Object[]{pwd, phone,SysStatic.NORMALDELFLAG});
	}
	
	/**
	 * 获取用户详细信息
	 * @param userId 用户id
	 * @return
	 */
	@Override
	public User showUserDetail(String userId) {
		String hql = "from User o where o.id = ? and o.delFlag = ?";
		return (User) this.userDAO.findObject(hql, new Object[]{userId,SysStatic.NORMALDELFLAG});
	}
	
	/**
	 * 更新前端类型
	 * @param userId 用户id
	 * @param frontType 前端类型 
	 */
	@Override
	public void updateFrontType(String userId, String frontType) throws BizException {
		String hql = "update User o set o.frontType = ? where o.id = ? and o.delFlag = ?";
		this.userDAO.update(hql, new Object[]{frontType, userId,SysStatic.NORMALDELFLAG});
	}

	@Override
	public int updatePhoneAndAccount(String userId, String account, String phone) throws BizException {
		String hql = "update User o set o.account = ?,o.phone = ? where o.id = ? and o.delFlag = ?";
		return this.userDAO.update(hql, new Object[]{account,phone, userId,SysStatic.NORMALDELFLAG});

		
	}

	@Override
	public int updatePasswordByAccount(String pwd, String account) throws BizException {
		String hql = "update User o set o.password = ? where o.account = ? and o.delFlag = ?";
		return this.userDAO.update(hql, new Object[]{pwd,account,SysStatic.NORMALDELFLAG});
	}
}